﻿using System;
using System.Globalization;
using System.Xml.Serialization;

namespace SvnFlow
{
    public class HistoryItem
    {
        [XmlAttribute]
        public string Time 
        { 
            get { return ParsedTime.ToString(SvnHelper.SerializedTimeFormat); }
            set { ParsedTime = DateTime.Parse(value, CultureInfo.InvariantCulture); }
        }
        [XmlAttribute]
        public string FromBranch { get; set; }
        [XmlAttribute]
        public long LastMergedRevision { get; set; }

        [XmlIgnore]
        public DateTime ParsedTime { get; set; }

        public bool Equals(HistoryItem obj)
        {
            if (ReferenceEquals(null, obj)) return false;
            if (ReferenceEquals(this, obj)) return true;

            return
                obj.ParsedTime == ParsedTime
                && obj.FromBranch == FromBranch
                && obj.LastMergedRevision == LastMergedRevision;
        }

        public override bool Equals(object obj)
        {
            if (ReferenceEquals(null, obj)) return false;

            var obj1 = obj as HistoryItem;
            return !ReferenceEquals(null, obj1) && Equals(obj1);
        }

        public override int GetHashCode()
        {
            return (ParsedTime + "|" + FromBranch + "|" + LastMergedRevision).GetHashCode();
        }
    }
}
